/*
 * JerboJournal.java
 *
 * Created on April 11, 2008, 2:48 PM
 */

package jerbo;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.ComboBoxModel;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.ListCellRenderer;
import jerbo.util.event.ArticleEvent;
import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.ResourceMap;

/**
 *
 * @author  Luigi Paioro
 */
public class JerboJournal extends javax.swing.JDialog implements jerbo.util.event.ArticleListener {
    
    /** Creates new form JerboJournal
     * @param parent 
     */
    
    java.util.ResourceBundle appBundle = java.util.ResourceBundle.getBundle("jerbo/resources/JerboApp"); // NOI18N
    ResourceMap resourceMap = Application.getInstance(jerbo.JerboApp.class).getContext().getResourceMap(JerboJournal.class);
    
    public JerboJournal(java.awt.Frame parent) {
        
        super(parent);
        initComponents();
        cleanForm();
                
        class InsertAction extends AbstractAction {
            
            JerboJournal form = null;
            
            public InsertAction(JerboJournal form) {
                this.form = form;
            }

            public void actionPerformed(ActionEvent e) {
                this.form.register();
            }
            
        }
        
        KeyStroke insert = KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 0, false);
        KeyStroke escape = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
        
        javax.swing.Action insertAction = new InsertAction(this);
        javax.swing.Action escapeAction = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        };

        getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(insert, "INSERT");
        getRootPane().getActionMap().put("INSERT", insertAction);
        
        getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escape, "ESCAPE");
        getRootPane().getActionMap().put("ESCAPE", escapeAction);
        
        
        class ColorCellRenderer extends JLabel  implements ListCellRenderer {
            
            ResourceMap resourceMap = Application.getInstance(jerbo.JerboApp.class).getContext().getResourceMap(JerboJournal.class);
            
            Color c1 = new Color(135, 206, 250);
            Color c2 = new Color(244, 164, 96);
            Color c3 = new Color(255, 215, 0);
             
            public Component getListCellRendererComponent(JList list, Object value,
                int index, boolean isSelected, boolean cellHasFocus) {

                setText((String) value);
                setOpaque(true);
                setForeground(Color.BLACK);
                
                if (isSelected) {
                    
                    setBackground(c3);
                    
                } else {
                
                    if (getSign((String) value) == 1) {
                        setBackground(c1);
                    } else {
                        setBackground(c2);
                    }
                }
                
                return this;
            }
            
            private int getSign(String value) {
                
                int sign = 1;
                int i = 0;
                
                while (resourceMap.containsKey("cause[" + i + "].name")) {
                    if (resourceMap.getString("cause[" + i + "].name").equals(value)) {
                        sign = resourceMap.getInteger("cause[" + i + "].sign");
                        break;
                    }
                    i++;
                }
                return sign;
            }
            
        }
        
        causeComboBox.setRenderer(new ColorCellRenderer());
    }
    
    public ComboBoxModel getCausesComboBoxModel() {
        
        Vector causes = new Vector();
        
        int i = 0;
        
        while (resourceMap.containsKey("cause[" + i + "].name")) {
            causes.addElement(resourceMap.getString("cause[" + i + "].name"));
            i++;
        }
                
        ComboBoxModel cbm = new javax.swing.DefaultComboBoxModel(causes);
        
        return cbm;
    }
    
    public int getCurrentSign() {
        
        int sign = 1;
        
        String cause = (String) causeComboBox.getModel().getSelectedItem();
        
        int i = 0;
        while (resourceMap.containsKey("cause[" + i + "].name")) {
            if (resourceMap.getString("cause[" + i + "].name").equals(cause)) {
                sign = resourceMap.getInteger("cause[" + i + "].sign");
                break;
            }
            i++;
        }

        return sign;
    }

    @Action
    public void close() {
        this.dispose();
    }
    
    /**
     * 
     */
    @Action
    public void register() {
        
//        String code = codeText.getText().trim().toUpperCase().replaceAll("'", "''");
        String desc = descriptionText.getText().trim().toUpperCase();
        
//        if (!code.equals("")) {
        if (!desc.equals("")) {
            
            try {
                Connection conn = Application.getInstance(jerbo.JerboApp.class).getConnection();
                Statement stat = conn.createStatement();
//                ResultSet result = stat.executeQuery("select code from articles where code='" + code + "'");
                ResultSet result = stat.executeQuery("select code from articles where description=\"" + desc + "\"");
                if (result.next()) {
                    
                    result.close();
                    
                    Integer expiry = null;
                    
//                    result = stat.executeQuery("select expiry from stock where article='" + code + "'");
                    result = stat.executeQuery("select expiry from stock where article=\"" + desc + "\"");
                    
                    if (this.getCurrentSign() > 0) {
                        
                        // LOAD
                        
                        expiry = this.getExpiryDate(JerboJournalExpiryBox.INPUT_DATE, null);
                        
                        if (expiry == null) {
                            //cleanForm();
                            return;
                        }
                        
                    } else {
                        
                        // DISCARD
                        
                        Vector dates = new Vector();
                        while (result.next())
                            dates.add(result.getString("expiry"));
                        
                        if (dates.size() > 1) {
                            expiry = this.getExpiryDate(JerboJournalExpiryBox.SELECT_DATE, dates);
                            if (expiry == null)  {
                                cleanForm();
                                return;
                            }
                        } else if (dates.size() == 1) {
                            
                            expiry = Integer.valueOf((String) dates.get(0));
                                    
                        } else {
                            JOptionPane.showMessageDialog(JerboApp.getApplication().getMainFrame(),
                                    resourceMap.getString("msg.empyStock"));
                            return;
                        }
                    }
                    
                    result.close();
                    
                    SimpleDateFormat dateFormat = new SimpleDateFormat(appBundle.getString("Application.date.format"));
                    Date date = dateFormat.parse(dateText.getText());
                    SimpleDateFormat dbDateFormat = new SimpleDateFormat("yyyyMMdd");
                    
                    String sql = "insert into journal values(NULL,";
                    sql += "" + dbDateFormat.format(date) + ",";
//                    sql += "'" + code + "',";
                    sql += "\"" + desc + "\",";
                    sql += (this.getCurrentSign() > 0 ? itemsText.getText().trim() : "-" + itemsText.getText().trim()) + ",";
                    sql += "'" + (String) causeComboBox.getModel().getSelectedItem() + "',";
                    sql += priceText.getText() + ",";
                    sql += "\"" + notesText.getText().toUpperCase() + "\")";
                    
                    stat.executeUpdate(sql);
                    
//                    result = stat.executeQuery("select count(expiry) as total from stock where article='" + code + "' and expiry=" + expiry.toString());
                    result = stat.executeQuery("select count(expiry) as total from stock where article=\"" + desc + "\" and expiry=" + expiry.toString());
                    
                    if (result.next()) {
                        
                        if (result.getInt("total") > 0) {
                            sql = "update stock set quantity = quantity ";
                            sql += (this.getCurrentSign() > 0 ? "+ " : "- ") +  itemsText.getText().trim();
                            sql += " where article = \"" + desc + "\" and expiry = " + expiry.toString();
//                            sql += " where article = '" + code + "' and expiry = " + expiry.toString();
                        } else {
//                            sql = "insert into stock values ('" + code + "', ";
                            sql = "insert into stock values (\"" + desc + "\", ";
                            sql += expiry.toString() + ", ";
                            sql += (this.getCurrentSign() > 0 ? 
                                itemsText.getText().trim() :
                                "-" + itemsText.getText().trim()) + ")";
                        }

                        stat.executeUpdate(sql);
                    }
                    
                    cleanForm();
                    
                } else {
                    JOptionPane.showMessageDialog(JerboApp.getApplication().getMainFrame(),
                            resourceMap.getString("msg.articleNotFound"));
                }
                if (result != null) result.close();
                if (stat != null) stat.close();
                
            } catch (ParseException ex) {
                Logger.getLogger(JerboJournal.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(JerboApp.getApplication().getMainFrame(),
                            resourceMap.getString("msg.unexpectedError")+": "+ex.getMessage());
              } catch (ClassNotFoundException ex) {
                Logger.getLogger(JerboArticles.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(JerboApp.getApplication().getMainFrame(),
                            resourceMap.getString("msg.unexpectedError")+": "+ex.getMessage());
            } catch (SQLException ex) {
                Logger.getLogger(JerboArticles.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(JerboApp.getApplication().getMainFrame(),
                            resourceMap.getString("msg.unexpectedError")+": "+ex.getMessage());
            }
            
        } else {
            JOptionPane.showMessageDialog(JerboApp.getApplication().getMainFrame(), resourceMap.getString("msg.noArticleSelected"));
        }
        
    }
    
    public void loadArticle() {
        
//        String code = codeText.getText().trim();
        String desc = descriptionText.getText().trim();
        
//        if (!code.equals(""))
        if (!desc.equals(""))
            
            try {
                Connection conn = Application.getInstance(jerbo.JerboApp.class).getConnection();
                Statement stat = conn.createStatement();
//                ResultSet result = stat.executeQuery("select description, price, vat from articles where code='" + code + "'");
                ResultSet result = stat.executeQuery("select code, price, vat, supplier_price from articles where description=\"" + desc + "\"");
                if (result.next()) {
//                    descriptionText.setText(result.getString("description"));
                    codeText.setText(result.getString("code"));
                    String format = appBundle.getString("Application.currency.format");
                    java.text.DecimalFormatSymbols symbols = new java.text.DecimalFormatSymbols();
                    symbols.setDecimalSeparator('.');
                    java.text.DecimalFormat decFormat = new java.text.DecimalFormat(format, symbols);
                    priceText.setText(decFormat.format(result.getFloat("price")));
                    priceSupplierText.setText(decFormat.format(result.getFloat("supplier_price")));
                    vatLabel.setText(result.getString("vat") + "%");
                } else {
                    codeText.setText("");
//                    descriptionText.setText("");
                    priceText.setText("");
                    priceSupplierText.setText("");
                    vatLabel.setText("");
                }
                if (result != null) result.close();
                if (stat != null) stat.close();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(JerboArticles.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(JerboApp.getApplication().getMainFrame(),
                            resourceMap.getString("msg.unexpectedError")+": "+ex.getMessage());
            } catch (SQLException ex) {
                Logger.getLogger(JerboArticles.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(JerboApp.getApplication().getMainFrame(),
                            resourceMap.getString("msg.unexpectedError")+": "+ex.getMessage());
            }
    }
    
    
    public void cleanForm() {
        dateText.setText(new java.text.SimpleDateFormat(resourceMap.getString("Application.date.format")).format(new java.util.Date()));
        codeText.setText("");
        codeText.requestFocus();
        descriptionText.setText("");
        priceText.setText("");
        priceSupplierText.setText("");
        vatLabel.setText("");
        itemsText.setText("1");
        notesText.setText("");
    }
    
    
    public Integer getExpiryDate(boolean mode, Vector dates) throws ParseException {
        if (journalExpiryBox == null) {
            JFrame mainFrame = JerboApp.getApplication().getMainFrame();
            journalExpiryBox = new JerboJournalExpiryBox(mainFrame, mode);
            journalExpiryBox.setLocationRelativeTo(mainFrame);
        } else {
            journalExpiryBox.setMode(mode);
        }
        if (mode == JerboJournalExpiryBox.SELECT_DATE) journalExpiryBox.setDates(dates);
        JerboApp.getApplication().show(journalExpiryBox);
        return journalExpiryBox.getResultDate();
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        codeLabel = new javax.swing.JLabel();
        codeText = new javax.swing.JTextField();
        itemsLabel = new javax.swing.JLabel();
        itemsText = new javax.swing.JTextField();
        causeLabel = new javax.swing.JLabel();
        causeComboBox = new javax.swing.JComboBox();
        closeButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();
        notesScrollPane = new javax.swing.JScrollPane();
        notesText = new javax.swing.JTextArea();
        notesLabel = new javax.swing.JLabel();
        dateLabel = new javax.swing.JLabel();
        try {
            javax.swing.text.MaskFormatter mf = new javax.swing.text.MaskFormatter(appBundle.getString("Application.date.mask"));
            mf.setPlaceholderCharacter('0');
            dateText = new javax.swing.JFormattedTextField(mf);
        } catch (java.text.ParseException pEx) {
            pEx.printStackTrace();
        }
        vatLabel = new javax.swing.JLabel();
        java.util.ResourceBundle appBundle = java.util.ResourceBundle.getBundle("jerbo/resources/JerboApp"); // NOI18N
        java.text.DecimalFormatSymbols symbols = new java.text.DecimalFormatSymbols();
        symbols.setDecimalSeparator('.');
        priceText = new javax.swing.JFormattedTextField(new java.text.DecimalFormat(appBundle.getString("Application.currency.format"), symbols));
        descriptionText = new javax.swing.JTextField();
        descriptionLabel = new javax.swing.JLabel();
        priceSupplierText = new javax.swing.JFormattedTextField(new java.text.DecimalFormat(appBundle.getString("Application.currency.format"), symbols));

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(jerbo.JerboApp.class).getContext().getResourceMap(JerboJournal.class);
        setTitle(resourceMap.getString("title")); // NOI18N
        setName("Form"); // NOI18N
        setResizable(false);

        codeLabel.setText(resourceMap.getString("codeLabel.text")); // NOI18N
        codeLabel.setName("codeLabel"); // NOI18N

        codeText.setName("codeText"); // NOI18N
        codeText.setNextFocusableComponent(itemsText);

        itemsLabel.setText(resourceMap.getString("itemsLabel.text")); // NOI18N
        itemsLabel.setName("itemsLabel"); // NOI18N

        itemsText.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        itemsText.setText("1"); // NOI18N
        itemsText.setName("itemsText"); // NOI18N
        itemsText.setNextFocusableComponent(causeComboBox);

        causeLabel.setText(resourceMap.getString("causeLabel.text")); // NOI18N
        causeLabel.setName("causeLabel"); // NOI18N

        causeComboBox.setBackground(new java.awt.Color(255, 255, 255));
        causeComboBox.setModel(getCausesComboBoxModel());
        causeComboBox.setName("causeComboBox"); // NOI18N
        causeComboBox.setNextFocusableComponent(notesText);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(jerbo.JerboApp.class).getContext().getActionMap(JerboJournal.class, this);
        closeButton.setAction(actionMap.get("close")); // NOI18N
        closeButton.setText(resourceMap.getString("closeButton.text")); // NOI18N
        closeButton.setName("closeButton"); // NOI18N
        closeButton.setNextFocusableComponent(dateText);

        okButton.setAction(actionMap.get("register")); // NOI18N
        okButton.setText(resourceMap.getString("okButton.text")); // NOI18N
        okButton.setName("okButton"); // NOI18N
        okButton.setNextFocusableComponent(closeButton);

        notesScrollPane.setName("notesScrollPane"); // NOI18N

        notesText.setColumns(20);
        notesText.setRows(5);
        notesText.setName("notesText"); // NOI18N
        notesText.setNextFocusableComponent(okButton);
        notesText.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                notesTextFocusLost(evt);
            }
        });
        notesScrollPane.setViewportView(notesText);

        notesLabel.setText(resourceMap.getString("notesLabel.text")); // NOI18N
        notesLabel.setName("notesLabel"); // NOI18N

        dateLabel.setText(resourceMap.getString("dateLabel.text")); // NOI18N
        dateLabel.setName("dateLabel"); // NOI18N

        dateText.setName("dateText"); // NOI18N
        dateText.setNextFocusableComponent(codeText);

        vatLabel.setBackground(new java.awt.Color(255, 255, 255));
        vatLabel.setForeground(new java.awt.Color(0, 51, 204));
        vatLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        vatLabel.setName("vatLabel"); // NOI18N
        vatLabel.setOpaque(true);

        priceText.setForeground(new java.awt.Color(255, 0, 0));
        priceText.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        priceText.setName("priceText"); // NOI18N

        descriptionText.setName("descriptionText"); // NOI18N
        descriptionText.setNextFocusableComponent(itemsText);
        descriptionText.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                descriptionTextFocusLost(evt);
            }
        });
        descriptionText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                descriptionTextKeyPressed(evt);
            }
        });

        descriptionLabel.setText(resourceMap.getString("descriptionLabel.text")); // NOI18N
        descriptionLabel.setName("descriptionLabel"); // NOI18N

        priceSupplierText.setForeground(resourceMap.getColor("priceSupplierText.foreground")); // NOI18N
        priceSupplierText.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        priceSupplierText.setName("priceSupplierText"); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(dateLabel)
                    .add(dateText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 131, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(codeLabel)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, codeText, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, descriptionText, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                    .add(itemsLabel)
                    .add(notesLabel)
                    .add(notesScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(okButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(closeButton))
                    .add(descriptionLabel)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(priceText)
                            .add(itemsText, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE))
                        .add(12, 12, 12)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(priceSupplierText, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(vatLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 76, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(causeLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 77, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(causeComboBox, 0, 241, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(dateLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(dateText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(codeLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(codeText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(12, 12, 12)
                .add(descriptionLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(descriptionText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(priceText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(priceSupplierText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(vatLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 18, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(16, 16, 16)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(itemsLabel)
                    .add(causeLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(itemsText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(causeComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(notesLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(notesScrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(closeButton)
                    .add(okButton))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void notesTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_notesTextFocusLost
        notesText.setText(notesText.getText().toUpperCase());
    }//GEN-LAST:event_notesTextFocusLost

    private void descriptionTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_descriptionTextFocusLost
        this.loadArticle();
}//GEN-LAST:event_descriptionTextFocusLost

    private void descriptionTextKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_descriptionTextKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER)
            this.loadArticle();
}//GEN-LAST:event_descriptionTextKeyPressed
    
private JerboJournalExpiryBox journalExpiryBox;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox causeComboBox;
    private javax.swing.JLabel causeLabel;
    private javax.swing.JButton closeButton;
    private javax.swing.JLabel codeLabel;
    private javax.swing.JTextField codeText;
    private javax.swing.JLabel dateLabel;
    private javax.swing.JFormattedTextField dateText;
    private javax.swing.JLabel descriptionLabel;
    private javax.swing.JTextField descriptionText;
    private javax.swing.JLabel itemsLabel;
    private javax.swing.JTextField itemsText;
    private javax.swing.JLabel notesLabel;
    private javax.swing.JScrollPane notesScrollPane;
    private javax.swing.JTextArea notesText;
    private javax.swing.JButton okButton;
    private javax.swing.JFormattedTextField priceSupplierText;
    private javax.swing.JFormattedTextField priceText;
    private javax.swing.JLabel vatLabel;
    // End of variables declaration//GEN-END:variables

    public void onArticleSelect(ArticleEvent evt) {
//        codeText.setText(evt.getArticle());
        descriptionText.setText(evt.getArticle());
        this.loadArticle();
    }
    
}